/*
* @Author: song
* @Description:脚本流水线行为基类
* @Date: 2024年04月18日 星期四 14:04:22
* @Modify:
*/

using System;

namespace SCore.Function.ScriptPipeline
{
    public abstract class ScrpitPipelineAction
    {
        private float _nextTime;
        private ScriptPipeline _scriptPipeline;
        private Action _complete;
        private bool _finish;

        protected ScrpitPipelineAction(float nextTime = 0f)
        {
            _finish = false;
            _nextTime = nextTime;
        }

        protected abstract bool Action(float dt);

        public virtual void SUpdate(float dt)
        {
            if (_nextTime >= 0)
            {
                _nextTime -= dt;
                if (_nextTime < 0)
                {
                    _scriptPipeline?.Next();
                }
            }

            //等待  next 和自己都完成
            if (!_finish)
            {
                _finish = Action(dt);
                if (_finish)
                {
                    _complete?.Invoke();
                    _complete = null;
                }
            }

            if (_finish && _nextTime < 0)
            {
                _scriptPipeline?.End(this);
            }
        }

        public virtual void Config(ScriptPipeline sPipeline)
        {
            _scriptPipeline = sPipeline;
        }

        public ScrpitPipelineAction Complete(Action action)
        {
            _complete = action;
            return this;
        }
    }
}